/*
 * 从数据库读指定卡号的交易数据并做XYPlot
 * 卡号在main中已写死
 */
package view;


import java.awt.BasicStroke;
import java.util.ArrayList;

import model.NewPayRecord;
import model.TotalRecord;

import org.jfree.chart.labels.StandardXYToolTipGenerator;
import org.jfree.chart.renderer.xy.XYBarRenderer;
import org.jfree.chart.renderer.xy.XYItemRenderer;
import org.jfree.chart.renderer.xy.XYLineAndShapeRenderer;
import org.jfree.chart.renderer.xy.XYStepRenderer;
import org.jfree.data.time.Day;
import org.jfree.data.time.Hour;
import org.jfree.data.time.Minute;
import org.jfree.data.time.Second;
import org.jfree.data.time.TimeSeries;
import org.jfree.data.time.TimeSeriesCollection;
import org.jfree.data.xy.IntervalXYDataset;
import org.jfree.data.xy.XYDataset;
import org.jfree.ui.RefineryUtilities;

import test.DrawBalance;
import view.jfreechartTest.DualAxisDemo1;
import view.jfreechartTest.ScatterPlotDemo1;
import view.jfreechartTest.TimeSeriesExample;
import view.jfreechartTest.XYBarChartDemo1;
import view.jfreechartTest.XYStepRendererDemo1;

import control.Parser;
import exception.ViewException;

public class DrawXYFromDatabase {

	public DrawXYFromDatabase(){
		
	}
	
	//被用于画单卡余额，修改为StepRenderer图
	public DrawXYFromDatabase(String cardNo,String chartName, String legendName){
		/* 旧版本代码
		TimeSeriesExample tse = new TimeSeriesExample(chartName);

		TimeSeriesCollection dataset = new TimeSeriesCollection();
		dataset = this.drawXYPlotFromResultSet(quaryID);
	
		tse.DrawXYChart(dataset);*/
		TimeSeriesCollection dataset = DrawXYFromDatabase.drawXYPlotFromResultSet(cardNo, legendName);
        XYStepRendererDemo1 xysteprendererdemo1 = new XYStepRendererDemo1(chartName, dataset);
        xysteprendererdemo1.pack();
        RefineryUtilities.centerFrameOnScreen(xysteprendererdemo1);
        xysteprendererdemo1.setVisible(true);
		
	}

	public void DrawSingleChart(int quarySelect, String tableName) {
		if(quarySelect%2 == 0){                     //recharge柱状图
			//get dataset
			String chartName = "充值记录柱状图";
			String legendName = "充值金额";
			IntervalXYDataset dataset = DrawXYFromDatabase.drawXYPlotFromTable(tableName, "recharge",legendName);
			
			XYBarChartDemo1 xybarchartdemo1 = new XYBarChartDemo1(chartName, dataset);
	        xybarchartdemo1.pack();
	        RefineryUtilities.centerFrameOnScreen(xybarchartdemo1);
	        xybarchartdemo1.setVisible(true);
		}
		else if(quarySelect%3 == 0) {               //Pay散点图
			//get dataset
			String viewName = "merge_pospay";
			String chartName = "消费散点图";
			String legendName = "消费金额";
			TimeSeriesCollection dataset = DrawXYFromDatabase.drawXYPlotFromTable(tableName, "pay",legendName);

			ScatterPlotDemo1 scatterplotdemo1 = new ScatterPlotDemo1(chartName, dataset);
	        scatterplotdemo1.pack();
	        RefineryUtilities.centerFrameOnScreen(scatterplotdemo1);
	        scatterplotdemo1.setVisible(true);
		}
		else if(quarySelect%5 == 0){               //整体余额折线图 
			//get dataset
			String chartName = "整体余额变化图";
			String legendName = "整体余额"; 
			try {
				TimeSeriesCollection dataset = DrawBalance.drawTotalBalance(tableName,legendName);
				DrawBalance.drawStepChart(chartName,dataset);
			} catch (ViewException e) {
				e.printStackTrace();
			}
		}
		else if(quarySelect%7 == 0) {                 //最后一个查询，待定
			String viewName = "merge_onlinepay";
			String chartName = "查询金额图";
			String legendName = "查询金额";
			TimeSeriesCollection dataset = DrawXYFromDatabase.drawXYPlotFromTable(tableName,"query",legendName);
			
			
			ScatterPlotDemo1 scatterplotdemo1 = new ScatterPlotDemo1(chartName, dataset);
	        scatterplotdemo1.pack();
	        RefineryUtilities.centerFrameOnScreen(scatterplotdemo1);
	        scatterplotdemo1.setVisible(true);
		}
	}
	
	public void DrawMultiChart(int quarySelect, String tableName) {
		//TODO: 
		ArrayList<XYDataset> datasetList = new ArrayList<XYDataset>();
		ArrayList<XYItemRenderer> rendererList = new ArrayList<XYItemRenderer>();
		String chartName = "";
		if(quarySelect%2 == 0){                     //deposit(recharge)柱状图
			//get dataset
			String legendName = "充值金额";
			chartName += "充值";
			IntervalXYDataset dataset = DrawXYFromDatabase.drawXYPlotFromTable(tableName, "recharge",legendName);
			datasetList.add(dataset);
			
			XYBarRenderer renderer = new XYBarRenderer();
			rendererList.add(renderer);
			
		}
		if(quarySelect%3 == 0){               //onlinePay(3) 散点图 
			//get dataset
			String viewName = "merge_onlinepay";
			if(!chartName.equals(""))  chartName += ",";
			chartName += "支出金额";
			String legendName = "支出金额";
			TimeSeriesCollection dataset = DrawXYFromDatabase.drawXYPlotFromTable(tableName, "pay",legendName);
			datasetList.add(dataset);
			
			XYLineAndShapeRenderer renderer = new XYLineAndShapeRenderer(false, true);
			rendererList.add(renderer);

		}
		if(quarySelect%5 == 0) {               //posPay散点图
			
			
			//get dataset
			if(!chartName.equals(""))  chartName += ",";
			chartName += "整体余额";
			String legendName = "整体余额";
			try {
				TimeSeriesCollection dataset = DrawBalance.drawTotalBalance(tableName,legendName);
				datasetList.add(dataset);
			}catch (ViewException e) {
				e.printStackTrace();
			}
			
			XYStepRenderer xysteprenderer = new XYStepRenderer();
	        xysteprenderer.setBaseShapesVisible(true);
	        xysteprenderer.setSeriesStroke(0, new BasicStroke(2.0F));
	        xysteprenderer.setSeriesStroke(1, new BasicStroke(2.0F));
	        xysteprenderer.setBaseToolTipGenerator(new StandardXYToolTipGenerator());
	        xysteprenderer.setDefaultEntityRadius(6);
			rendererList.add(xysteprenderer);

		}
		if(quarySelect%7 == 0)				  //查询 暂时没用
		{
			//get dataset
			String viewName = "merge_pospay";
			if(!chartName.equals(""))  chartName += ",";
			chartName += "查询金额";
			String legendName = "查询金额"; 
			TimeSeriesCollection dataset = DrawXYFromDatabase.drawXYPlotFromTable(tableName,"query",legendName);
			datasetList.add(dataset);
			XYLineAndShapeRenderer renderer = new XYLineAndShapeRenderer(false, true);
			rendererList.add(renderer);
			
		}
		//draw multiple pic
		chartName += "变化图";
		DualAxisDemo1 newChart = new DualAxisDemo1(chartName,datasetList,rendererList);
		newChart.pack();
        RefineryUtilities.centerFrameOnScreen(newChart);
        newChart.setVisible(true);
	}
	
	
	//modified by zy: for use in test/XYStepRendererDemo1.java:createDataset().
	@SuppressWarnings("deprecation")
	public static TimeSeriesCollection drawXYPlotFromResultSet(String quaryID, String legendName) {
		
		// 设置画图参数
		//String chartName = "单卡余额";

		TimeSeriesCollection dataset = new TimeSeriesCollection();

		TimeSeries pop = new TimeSeries(legendName, Second.class);

		try {
			/*int j = 0;
			j = rsmd.getColumnCount();
	
			for (int k = 0; k < j; k++) {

			}*/


			int i =1;
			
			for(NewPayRecord record : Parser.getNewPayRecord(quaryID)) {
				
				pop.addOrUpdate(new Second(record.getSecond(), 
							new Minute(record.getMinute(), 
								new Hour(record.getHour(), 
									new Day(record.getDate(), record.getMonth(), record.getYear())))), 
										record.getBalanceValue());


				i++;
				

			}
			dataset.addSeries(pop);
///再添加一个结束时间的点，值保持最后一个点的值
///			
///			
		} catch (Exception e) {

			e.printStackTrace();

		}
		return dataset;

	}
	
	public TimeSeriesCollection drawXYPlotFromResultSet(int quarySelect,String legendName) {
		
		// 设置画图参数
		//String chartName = "整体余额";

		TimeSeriesCollection dataset = new TimeSeriesCollection();

		@SuppressWarnings("deprecation")
		TimeSeries pop = new TimeSeries(legendName, Second.class);

		try {
			/*int j = 0;
			j = rsmd.getColumnCount();
	
			for (int k = 0; k < j; k++) {


			}*/

			
			for(TotalRecord record : Parser.getTotalRecord(quarySelect)) {
				
				pop.addOrUpdate(new Day(record.getDate(), record.getMonth(), record.getYear()), 
										record.getBalanceValue());

				
			}
			dataset.addSeries(pop);
///再添加一个结束时间的点，值保持最后一个点的值
///			
///			
		} catch (Exception e) {

			e.printStackTrace();

		}
		return dataset;

	}

	public static TimeSeriesCollection drawXYPlotFromViewSet(String viewName, String legendName) {
		// 设置画图参数
		//String chartName = "余额分析1";

		TimeSeriesCollection dataset = new TimeSeriesCollection();

		@SuppressWarnings("deprecation")
		TimeSeries pop = new TimeSeries(legendName, Second.class);

		try {

			for(NewPayRecord record : Parser.getViewRecord(viewName,"0")) {
				
				pop.addOrUpdate(new Day(record.getDate(), record.getMonth(), record.getYear()), 
										record.getBalanceValue());

				


			}
			dataset.addSeries(pop);
///再添加一个结束时间的点，值保持最后一个点的值
///			
///			
		} catch (Exception e) {

			e.printStackTrace();

		}
		return dataset;
	}
	
	
	//get data collection from table result.
	public static TimeSeriesCollection drawXYPlotFromTable(String tableName, String mark, String legendName) {
		// 设置画图参数

		TimeSeriesCollection dataset = new TimeSeriesCollection();

		@SuppressWarnings("deprecation")
		TimeSeries pop = new TimeSeries(legendName, Second.class);

		try {

			for(NewPayRecord record : Parser.getTableRecord(tableName, mark, "0")) {
				
				pop.addOrUpdate(new Day(record.getDate(), record.getMonth(), record.getYear()), 
										record.getBalanceValue());

				


			}
			dataset.addSeries(pop);
///再添加一个结束时间的点，值保持最后一个点的值
///			
///			
		} catch (Exception e) {

			e.printStackTrace();

		}
		return dataset;
	}
	
	

//	public static void main(String args[]) {
//
//		DrawXYFromDatabase newChart = new DrawXYFromDatabase();
//
//		String chartName = "单卡余额";
//		String quaryID = "'5088010100000011243'";
//		// 设置画图参数
//		TimeSeriesExample tse = new TimeSeriesExample(chartName);
//
//		TimeSeriesCollection dataset = new TimeSeriesCollection();
//		dataset = newChart.drawXYPlotFromResultSet(quaryID);
////		dataset = newChart.drawXYPlotFromResultSet();
//	
//		tse.DrawXYChart(dataset);
//
//	}
//	
//	String chartName = "单卡余额";



}
